package com.lw.controller;

import com.lw.common.IJsonResult;
import com.lw.pojo.Users;
import com.lw.service.UserServiceImpl;
import com.lw.utils.MD5Utils;
import com.lw.utils.MyStringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

@RestController
@Api(value="用户注册登录",tags = {"注册和登录"})
public class RegistLoginController {

    @Autowired
    private UserServiceImpl userService;

    @PostMapping("/regist")
    @ApiOperation(value="注册",notes = "注册接口")
    public IJsonResult regist(@RequestBody Users users){
        if(MyStringUtil.hasEmpty(users.getPassword(),users.getUsername())){
            return IJsonResult.errorMsg("用户名或密码不能为空");
        }

        //2. 判断用户名是否存在
        boolean has = this.userService.queryUsernameIsExist(users.getUsername());
        if(!has){
            users.setPassword(MD5Utils.getMD5Str(users.getPassword()));
        }

        return IJsonResult.errorMsg("失败");
    }

    //这里用三个目录主要是为了测试哪个生效了
    @Value("${file.base.path:/temp/user/images/}")
    private String fileBasePath = "/temp/files";//文件上传路径

    @ApiOperation(value="上传头像",notes = "上传头像接口")
    @ApiImplicitParam(name="userId",value = "用户ID",required = true
            ,dataType = "String",paramType = "query")
    @PostMapping(value = "/user/uploadFace")
    public IJsonResult uploadFace(String userId,@RequestParam("file") MultipartFile[] files) throws Exception{
        String fileSpace = fileBasePath+ File.separator+"images"+File.separator;

        if(files!=null&&files.length>0){
            //Lpk26pk8YQEz41992fffbcd1a6a3794bf7732c90c60b.png
           String fileName = files[0].getOriginalFilename();
           String filePath = fileSpace+fileName;//文件保存的路径

            File faceFile = new File(filePath);
            if(!faceFile.getParentFile().exists()){//目录不存在则创建
                faceFile.getParentFile().mkdirs();
            }
            InputStream is = files[0].getInputStream();
            FileOutputStream fos = new FileOutputStream(filePath);

//            IOUtils.copy(is,fos);

            //下面这段代码等同于上面IOUitls那一句
            byte[] buffer = new byte[4096];
            int n =0;
            while((n=is.read(buffer))!=-1){
                fos.write(buffer,0,n);
            }

            is.close();
            fos.flush();//网络环境需要清空管道缓存，如果是本地这步可以省略
            fos.close();
        }

        return IJsonResult.ok();
    }

}
